package ar.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import ar.Location.Map.MyGeocoder;
import ar.Location.Map.MyOverlayItem;
import ar.Location.Route.Road;
import ar.Location.Route.RoadProvider;
import ar.activity.R.string;

import com.google.android.maps.GeoPoint;

public class DirectionsActivity extends BaseActivity {

	public static boolean modeDirections = false;
	public static boolean chooseStartPoint = false;
	public static boolean chooseEndPoint = false;
	public static GeoPoint startPoint = null;
	public static GeoPoint endPoint = null;

	ProgressDialog progressDialog = null;
	EditText editTextStartPoint = null;
	EditText editTextEndPoint = null;
	ImageView imgViewStartPoint = null;
	ImageView imgViewEndPoint = null;
	Spinner spinnerDirections = null;
	ArrayAdapter<CharSequence> arrayDataSource = null;

	MyGeocoder myGeoCoder = null;

	Button btnGo = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.directions);
		super.onCreate(savedInstanceState);
		if (myGeoCoder == null) {
			myGeoCoder = new MyGeocoder(DirectionsActivity.this);
		}
		tvWhatScreen.setText(res.getString(string.directions));
		initView();
		initSpinnerData();
	}
	
	private void initSpinnerData() {
		// TODO Auto-generated method stub
		spinnerDirections = (Spinner) findViewById(R.id.spinnerDataDirections);
		arrayDataSource  = ArrayAdapter
				.createFromResource(this, R.array.data_directions_array,
						android.R.layout.simple_spinner_item);
		arrayDataSource
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerDirections.setAdapter(arrayDataSource);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (startPoint != null) {
			editTextStartPoint.setText(res.getString(string.pointOnMap));
		} else {
			editTextStartPoint.setText(res.getString(string.hintStartPoint));
		}
		if (endPoint != null) {
			editTextEndPoint.setText(res.getString(string.pointOnMap));
		} else {
			editTextEndPoint.setText(res.getString(string.hintEndPoint));
		}

	}

	private void initView() {
		// TODO Auto-generated method stub

		editTextStartPoint = (EditText) findViewById(R.id.editTextStartPoint);
		editTextEndPoint = (EditText) findViewById(R.id.editTextEndPoint);

		imgViewStartPoint = (ImageView) findViewById(R.id.imgViewSelectStartPoint);
		imgViewStartPoint.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				modeDirections = true;
				chooseStartPoint = true;
				chooseEndPoint = false;
				Toast.makeText(DirectionsActivity.this,
						res.getString(string.selectStartPoint), Toast.LENGTH_SHORT)
						.show();
				Intent intent = new Intent(getApplicationContext(),
						ARMapActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

		imgViewEndPoint = (ImageView) findViewById(R.id.imgViewSelectEndPoint);
		imgViewEndPoint.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				modeDirections = true;
				chooseStartPoint = false;
				chooseEndPoint = true;
				Toast.makeText(DirectionsActivity.this,
						res.getString(string.selectEndPoint), Toast.LENGTH_SHORT)
						.show();
				Intent intent = new Intent(getApplicationContext(),
						ARMapActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});

		btnGo = (Button) findViewById(R.id.btnGo);
		btnGo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String strStartPoint = editTextStartPoint.getText()
						.toString();
				final String strEndPoint = editTextEndPoint.getText()
						.toString();
				if (strStartPoint.length() > 0 && strEndPoint.length() > 0) {
					progressDialog = ProgressDialog.show(
							DirectionsActivity.this, res.getString(string.processing),
							res.getString(string.findingRoute), true, false);

					Thread threadDirections = new Thread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							if (!strStartPoint.equals(res.getString(string.pointOnMap))) {
								MyOverlayItem overlayStartPoint = myGeoCoder
										.search(strStartPoint);
								if (overlayStartPoint != null) {
									startPoint = overlayStartPoint.getPoint();
								}
							}

							if (!strEndPoint.equals(res.getString(string.pointOnMap))) {
								MyOverlayItem overlayEndPoint = myGeoCoder
										.search(strEndPoint);
								if (overlayEndPoint != null) {
									endPoint = overlayEndPoint.getPoint();
								}
							}

							if (startPoint != null && endPoint != null) {
								float fromLatitude = (float) ((startPoint
										.getLatitudeE6() * 1.0) / 1E6);
								float fromLongitude = (float) ((startPoint
										.getLongitudeE6() * 1.0) / 1E6);
								float toLatitude = (float) ((endPoint
										.getLatitudeE6() * 1.0) / 1E6);
								float toLongitude = (float) ((endPoint
										.getLongitudeE6() * 1.0) / 1E6);

								int mode = spinnerDirections
										.getSelectedItemPosition();

								Road mRoad = RoadProvider.getRoute(
										fromLatitude, fromLongitude,
										toLatitude, toLongitude, mode);
								
								progressDialog.dismiss();
								
								Message msg = new Message();
								msg.obj = mRoad;
								if (ARMapActivity.handleDirections != null) {
									ARMapActivity.handleDirections
											.sendMessage(msg);
								}
								
								finish();
							}
							modeDirections = false;
						}
					});
					threadDirections.start();
				} else {
					Toast.makeText(DirectionsActivity.this,
							res.getString(string.inputEmpty), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	public static void resetParams() {
		modeDirections = false;
		chooseStartPoint = false;
		chooseEndPoint = false;
		startPoint = null;
		endPoint = null;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		resetParams();
		finish();
	}
	
	@Override
	protected void onUpdateUI() {
		// TODO Auto-generated method stub
		tvWhatScreen.setText(res.getString(string.directions));
		if (startPoint != null) {
			editTextStartPoint.setText(res.getString(string.pointOnMap));
		} else {
			editTextStartPoint.setText(res.getString(string.hintStartPoint));
		}
		if (endPoint != null) {
			editTextEndPoint.setText(res.getString(string.pointOnMap));
		} else {
			editTextEndPoint.setText(res.getString(string.hintEndPoint));
		}
		initSpinnerData();
	}
}
